package com.example.androidgeneric;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

public class F_TestViewPager extends Fragment {

	ViewPager pager;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View rootView = inflater.inflate(R.layout.f_testviewpager, null);
		pager = (ViewPager) rootView.findViewById(R.id.pager);

		return rootView;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		setPager();
	}

	private void setPager() {
		
		DisplayMetrics outMetrics = new DisplayMetrics();
		getActivity().getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
		int display_width = outMetrics.widthPixels;
		int display_height = (int) (outMetrics.widthPixels - (2 * ValueFormat.DPtoPX(getActivity(), 24)));
		LinearLayout.LayoutParams param = new LayoutParams(display_width, display_height);
		
		int padding = (int) ValueFormat.DPtoPX(getActivity(), 24);
		
		pager.setPadding(padding, 0, padding, 0);
		pager.setLayoutParams(param);
		pager.setAdapter(new mAdapter());
		pager.setClipToPadding(false);
		pager.setPageMargin((int) ValueFormat.DPtoPX(getActivity(), 8));
	}

	private class mAdapter extends PagerAdapter {

		private int[] res = { R.drawable.test1, R.drawable.test2,
				R.drawable.test3 };

		@Override
		public int getCount() {
			return 3;
		}

		@Override
		public Object instantiateItem(View container, int position) {

			RoundImageView img = new RoundImageView(getActivity());
			img.setScaleType(ScaleType.CENTER_CROP);
			Bitmap bm = BitmapFactory.decodeResource(getResources(), res[position]);
			img.setImageBitmap(getRoundedCornerBitmap(getActivity(), bm));

			((ViewPager) container).addView(img, 0);

			return img;
		}

		@Override
		public void destroyItem(View container, int position, Object view) {
			// super.destroyItem(container, position, object);
			((ViewPager) container).removeView((View) view);
		}

		@Override
		public boolean isViewFromObject(View pager, Object obj) {
			return pager == obj;
		}
	}
	
	private class RoundImageView extends ImageView {

		public RoundImageView(Context context) {
			super(context);
			// TODO Auto-generated constructor stub
		}
		
		@Override
		protected void onDraw(Canvas canvas) {
			
			Path clipPath = new Path();
		    float radius = 30.0f;
		    float padding = radius / 2;
		    int w = this.getWidth();
		    int h = this.getHeight();
		    clipPath.addRoundRect(new RectF(padding, padding, w - padding, h - padding), radius, radius, Path.Direction.CW);
		    canvas.clipPath(clipPath);
		    super.onDraw(canvas);
			
			super.onDraw(canvas);
		}
	}
	
	private Bitmap getRoundedCornerBitmap(Context context, Bitmap bitmap) {
		Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
		Canvas canvas = new Canvas(output);
		
		final int color = 0xff424242;
		final Paint paint = new Paint();
		final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
		final RectF rectF = new RectF(rect);
		final float roundPx = ValueFormat.DPtoPX(context, 8);
		
		paint.setAntiAlias(true);
		canvas.drawARGB(0, 0, 0, 0);
		paint.setColor(color);
		canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
		
		paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
		canvas.drawBitmap(bitmap, rect, rect, paint);
		
		return output;
	}
}
